package com.bingxue.edu.framework.common.model;

import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Range;

/**
 * 分页查询条件
 *
 * @author panbo
 * @since 2024/6/26
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@Schema(description = "分页查询条件")
public class PageQuery extends SortQuery {

    /**
     * 默认页码：1
     */
    private static final int DEFAULT_PAGE = 1;

    /**
     * 默认每页条数：10
     */
    private static final int DEFAULT_SIZE = 10;

    /**
     * 默认总数：-1
     * 目的： 优化分页查询，只有在总数小于0时才去查总数
     */
    private static final long DEFAULT_TOTAL = -1;

    @Schema(description = "页码", example = "1")
    @Min(value = 1, message = "页码最小值为 {value}")
    private Integer page = DEFAULT_PAGE;

    @Schema(description = "每页条数", example = "10")
    @Range(min = 1, max = 1000, message = "每页条数（取值范围 {min}-{max}）")
    private Integer size = DEFAULT_SIZE;

    /**
     * 优化分页查询，只有在总数小于0时才去查总数
     */
    @Schema(description = "总数", example = "1000")
    private Long total = DEFAULT_TOTAL;

    /**
     * 基于分页查询条件转换为 MyBatis Flex 分页条件
     *
     * @param <T> 列表数据类型
     * @return MyBatis Flex 分页条件
     */
    public <T> Page<T> toPage() {
        return new Page<>(page, size, total);
    }
}
